﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Text;

namespace MyDataProcess
{
    public class DataProcess
    {
        public DataSet SqlService_Query(string sqlString, SqlConnection con)//从SQLService数据库里取数据存为数据集
        {
            string sqlstr = sqlString;
            try
            {
                DataSet ds = new DataSet();//创建dataset
                SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlstr, con);//创建dataadapter
                SqlCommandBuilder cb = new SqlCommandBuilder(dataAdapter);//使用Sqlcommand类和dataadapter类对数据库操作
                dataAdapter.Fill(ds, "info");
                return ds;
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public bool IsInts(string str)//判断字符是否为数字
        {
            return Regex.IsMatch(str, @"^-?\d+$");
        }
        public string GetMystring(DataSet ds)//数据库取数据来拼链接字符串
        {
            string constr = "Data Source=" + ds.Tables[0].Rows[0]["DBNAME"].ToString()
                + ";User ID=" + ds.Tables[0].Rows[0]["SUSER"].ToString()
                + ";Password=" + ds.Tables[0].Rows[0]["SPASS"].ToString();
            return constr;
        }
        public string SqlService_updateSQL(string sqlString, SqlConnection con)//执行SqlService数据库单条更新语句
        {
            try
            {
                SqlCommand comd = new SqlCommand(sqlString, con);
                int count = comd.ExecuteNonQuery();
                if (count > 0)
                {
                    return "1";
                }
                else
                {
                    throw new Exception("更新了0行");
                }
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            }
        }

        public string SqlService_updateSQL(string sqlString, SqlConnection con, SqlTransaction mytrans)//执行SqlService数据库单条更新语句,并绑定进外层事务
        {
            try
            {
                SqlCommand comd = new SqlCommand(sqlString, con);
                comd.Transaction = mytrans;//绑定事务
                int count = comd.ExecuteNonQuery();
                if (count > 0)
                {
                    return "1";
                }
                else
                {
                    throw new Exception("更新了0行");
                }
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            }
        }
       
      
        public DataSet SplitDataSet(DataSet ds, int pageSize, int pageIndex) //分页 pageSize=每页条数 pageIndex=第几页
        {
            DataSet vds = new DataSet();
            vds = ds.Clone();
            if (pageIndex < 1) pageIndex = 1;//如果小于1，取第一页
            if ((ds.Tables[0].Rows.Count + pageSize) <= (pageSize * pageIndex)) pageIndex = 1;
            int fromIndex = pageSize * (pageIndex - 1);//开始行
            int toIndex = pageSize * pageIndex - 1; //结束行
            for (int i = fromIndex; i <= toIndex; i++)
            {
                if (i >= (ds.Tables[0].Rows.Count)) //到达这一行，退出
                    break;
                vds.Tables[0].ImportRow(ds.Tables[0].Rows[i]);
            }
            ds.Dispose();
            return vds;
        }
       

        public string jsonReturnHtmlInfo(int type, string errorinfo, DataSet ds)
        {
            if (type == 1)
            {
                string json = "[";
                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                {
                    json += "{";
                    for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
                    {
                        if (ds.Tables[0].Columns[k].ColumnName == "HTML")
                        {
                            string LoadStr = "";
                            if (ds.Tables[0].Rows[j][k] == null || ds.Tables[0].Rows[j][k].ToString() == "")
                            {
                                LoadStr = "";
                            }
                            else
                            {
                                byte[] loadBytes = (byte[])ds.Tables[0].Rows[j][k];
                                LoadStr = Encoding.Unicode.GetString(loadBytes, 0, loadBytes.Length).Replace("\"", "\\\"");
                            }
                            json += "\"" + ds.Tables[0].Columns[k].ColumnName + "\":\"" + LoadStr + "\"";
                        }
                        else
                        {
                            json += "\"" + ds.Tables[0].Columns[k].ColumnName + "\":\"" + ds.Tables[0].Rows[j][k].ToString() + "\"";
                        }
                        if (k != ds.Tables[0].Columns.Count - 1)
                            json += ",";
                    }
                    json += "}";
                    if (j != ds.Tables[0].Rows.Count - 1)
                        json += ",";
                }
                json += "]";
                return "{\"code\": 1,\"info\": \"success\",\"data\": " + json + "}";

            }
            else
            {
                return "{\"code\": 0,\"info\": \"error:" + errorinfo + "\"}";
            }
        }

        public string jsonReturnMedicalInfo(int type,string errorinfo,DataSet ds)
        {
            if(type==1)
            {
                string json = "[";
                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                {
                    json += "{";
                    for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
                    {
                        if (ds.Tables[0].Columns[k].ColumnName == "webcontent")
                        {
                            string LoadStr = "";
                            if (ds.Tables[0].Rows[j][k] == null || ds.Tables[0].Rows[j][k].ToString() == "")
                            {
                                LoadStr = "";
                            }
                            else
                            {
                                byte[] loadBytes = (byte[])ds.Tables[0].Rows[j][k];
                                LoadStr = Encoding.Unicode.GetString(loadBytes, 0, loadBytes.Length).Replace("\"", "\\\"");
                            }
                            json += "\""+ds.Tables[0].Columns[k].ColumnName + "\":\"" + LoadStr + "\"";
                        }
                        else
                        {
                            json += "\"" + ds.Tables[0].Columns[k].ColumnName + "\":\"" + ds.Tables[0].Rows[j][k].ToString() + "\"";
                        }
                        if (k != ds.Tables[0].Columns.Count - 1)
                            json += ",";
                    }
                    json += "}";
                    if (j != ds.Tables[0].Rows.Count - 1)
                        json += ",";
                }
                json += "]";
                return "{\"code\": 1,\"info\": \"success\",\"data\": " + json + "}";
                
            }
            else 
            {
               return "{\"code\": 0,\"info\": \"error:" + errorinfo + "\"}";
            }
        }
        public string jsonReturn(int type, string errorinfo, DataSet ds)
        {
            if (type == 1)
            {
                string json = "[";
                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                {
                    json += "{";
                    for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
                    {

                        json += "\"" + ds.Tables[0].Columns[k].ColumnName + "\":\"" + ds.Tables[0].Rows[j][k].ToString() + "\"";

                        if (k != ds.Tables[0].Columns.Count - 1)
                            json += ",";
                    }
                    json += "}";
                    if (j != ds.Tables[0].Rows.Count - 1)
                        json += ",";
                }
                json += "]";
                return "{\"code\": 1,\"info\": \"success\",\"data\": " + json + "}";

            }
            else
            {
                return "{\"code\": 0,\"info\": \"error:" + errorinfo + "\"}";
            }
        }
        public string jsonReturn(int type, string errorinfo)
        {
            if (type == 1)
            {

                return "{\"code\": 1,\"info\": \"success\"}";

            }
            else
            {
                return "{\"code\": 0,\"info\": \"error:" + errorinfo + "\"}";
            }
        }
       
    }  

}
